package com.qinchen.boot.controller;

import com.qinchen.boot.bean.CarProperties;
import com.qinchen.boot.bean.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qinchen
 * @date 2021/1/20 11:00 下午
 * 向未来的自己致敬
 * 描述：XXX
 */
@Slf4j
@RestController
public class HelloController {

    @Autowired
    private CarProperties carProperties;

    /**
     * http://localhost:8080/hello
     *
     * @return
     */
    @RequestMapping("/hello")
    public String handle01() {

        System.out.println("1");

        System.out.println("获取到配置信息" + carProperties.toString());

        log.info("测试 lombok @Slf4j");

        return "Hello, Spring Boot 2!!!!";
    }

    /**
     * 先看Controller中查映射关系，不能处理，再从静态资源查找
     * http://localhost:8080/img-0001.jpg
     *
     * @return
     */
    @RequestMapping("/img-0001.jpg")
    public String img0001() {

        log.info("测试 mvc 映射");

        return "测试 mvc 映射";
    }

    /**
     * 测试参数绑定
     * http://localhost:8080/car/1/owner/qinchen
     *
     * @return
     */
    @GetMapping("/car/{id}/owner/{username}")
    public Map getCar(@PathVariable("id") Integer id,
                      @PathVariable("username") String username,
                      @PathVariable Map<String, String> pv,
                      @RequestHeader("user-Agent")String userAgent,
                      @RequestHeader Map<String, Object> headers,
                      @CookieValue("_ga") Cookie cookie) {

        Map<String, Object> data = new HashMap<>();

        data.put("id", id);
        data.put("username", username);
        data.put("pv", pv);

        data.put("userAgent", userAgent);
        data.put("header", headers);

        data.put("cookie", cookie);

        return data;
    }

    @PostMapping("/save")
    public Map save(@RequestBody String body) {

        Map<String, Object> data = new HashMap<>();

        data.put("body", body);

        return data;
    }

    /**
     * 矩阵变量测试
     * http://localhost:8888/cars/sell;low=34;brand=mazida,audi,woerwo
     * 注意，默认不开启，需要自己配置WebMvcConfigurer
     *      且矩阵变量必须有url路径变量才可解析
     * @return
     */
    @GetMapping("/cars/{path}")
    public Map carsSell(@PathVariable("path")String path,
                        @MatrixVariable("low") Integer low,
                        @MatrixVariable("brand") List<String> brand){
        Map<String, Object> map = new HashMap();

        map.put("low", low);
        map.put("brand", brand);

        return map;
    }

    /**
     * 矩阵变量测试（多个同名矩阵变量名称）
     * http://localhost:8888/boss/qinchen;age=18/qisehua;age=25
     * 注意，默认不开启，需要自己配置WebMvcConfigurer
     *      且矩阵变量必须有url路径变量才可解析
     * @return
     */
    @GetMapping("/boss/{path1}/{path2}")
    public Map carsSell(@MatrixVariable(value = "age", pathVar = "path1") Integer age1,
                        @MatrixVariable(value = "age", pathVar = "path2") Integer age2){
        Map<String, Object> map = new HashMap();

        map.put("age1", age1);
        map.put("age2", age2);

        return map;
    }

    /**
     * 研究自定义对象参数解析器
     * ServletModelAttributeMethodProcessor
     * @param user
     * @return
     */
    @PostMapping("/save/user")
    public User saveUser(User user){
        log.info("研究对象参数接收器");
        return user;
    }

}
